package com.util;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region.Op;

import com.javenwong.android.engine.GameActivity; 

public class T {
	public static int SW;
	public static int SH;
	public static Matrix tmpMatrix = new Matrix();
	private static Rect tempRec = new Rect();
	private static Bitmap numImage;

	public static InputStream getResources(String path, String data) {
		InputStream is = null;
		try {
			is = GameActivity.gameActivity.getAssets().open(path + data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}

	public static InputStream getResourceAsStream(String path)
			throws IOException {
		if (path.startsWith("/")) {
			path = path.substring(1);
		}
		return GameActivity.gameActivity.getAssets().open(path);
	}

	public static Bitmap createImage(byte[] imageButton, int imageOffset,
			int imageLength) {
		return BitmapFactory.decodeByteArray(imageButton, imageOffset,
				imageLength);
	}

	public static void drawRegion(Canvas g, Bitmap[] imgNums, int num, int x,
			int y, Paint paint) {
		if (imgNums == null) {
			return;
		}
		int index = 0;
		String sNum = String.valueOf(num);
		for (int i = 0; i < sNum.length(); i++) {
			char value = sNum.charAt(i);
			index = value - 48;
			g.drawBitmap(imgNums[index], x, y, paint);
			x += 25;
		}
	}

	/**
	 * 进行字符串中间的分割(去空格)
	 * 
	 * @param str
	 *            要分割的字符串
	 * @param split
	 *            分割格式
	 * @return 分割后的字符串
	 */
	public static String[] splitString(String str, String split) {
		int n = str.indexOf(split);
		if (n < 0) {
			return new String[] { str };
		} else {
			return new String[] { str.substring(0, n).trim(),
					str.substring(n + 1) };
		}
	}

	/**
	 * 处理字符串
	 * 
	 * @param font
	 * @param text
	 * @param width
	 * @return
	 */
	public static List splitString(Paint font, String text, int width) {
		List talkVector = new ArrayList();
		// talkVector.removeAllElements();
		int ww = 0; // 字符串累加长度值
		int start = 0; // 截取字符串的开始值
		int color = 0; // 默认黑色
		boolean colorSet = false;
		List vectorLine = null;
		char[] chars = text.toCharArray();
		for (int i = 0; i < text.length(); i++) {

			// 读取字符串，累加长度，如果字符串超过屏幕框的长度就截取出放到数组里
			char ch = text.charAt(i);

			// 如果 colorSet 判断颜色
			// color = ??; // 根据颜色设置 color 值

			// 如果是 # 颜色开始
			if (colorSet) {
				// 判断颜色
				switch (ch) {
				case 'r':
					color = 0xFF0000; // 红色
					break;
				case 'b':
					color = 0xFF00; //
					break;
				case 'g':
					color = 0xFF; //
					break;
				}
			}
			// colorSet = true;
			if (ch == '#') {
				Text text2 = new Text();
				text2.setColor(color);
				text2.setText(text.substring(start, i));
				if (!colorSet) { // 开始
					colorSet = true;
					start = i + 2;
				} else { // 结束
					colorSet = false;
					color = 0;
					start = i + 1;
				}
				if (vectorLine == null) {
					vectorLine = new ArrayList();
					talkVector.add(vectorLine);
				}
				vectorLine.add(text2);
				// colorSet == true ? start = i+2 : start = i ;

				// start = i + 2;
			}

			// 如果 colorSet 把之前的字符加入本行
			ww += (int) font.measureText(chars, i, 1);

			if (ww > width || ch == '\n') { // 如果 超过宽度 把之前的字符加入本行, 新启一行
				// talkVector.addElement(vectorLine);
				Text text2 = new Text();
				text2.setColor(color);
				text2.setText(text.substring(start, i));
				if (vectorLine == null) {
					vectorLine = new ArrayList();
					talkVector.add(vectorLine);
				}
				vectorLine.add(text2);
				vectorLine = null;
				// vectorLine.removeAllElements();
				// System.out.println(i);
				if (ww > width) {
					start = i;
				} else if (ch == '\n') {
					start = i + 1;
				}
				ww = 0;
			}
		}

		// 控制NPC说的话不到一行
		if (ww <= width) {
			Text text2 = new Text();
			text2.setColor(color);
			// System.out.println("start   "+start);
			text2.setText(text.substring(start, text.length()));
			if (vectorLine == null) {
				vectorLine = new ArrayList();
				talkVector.add(vectorLine);
			}
			vectorLine.add(text2);
		}
		// System.out.println("line   " + vectorLine.size());
		// System.out.println(" talk   " + talkVector.size());
		return talkVector;
	}

	/**
	 * 处理切入点（切换地图）方法
	 * 
	 * @param string
	 *            字符串
	 * @return
	 */
	public static String[] splitPoint(String str) {
		String[] stringArray = new String[5];
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		int j = 0;
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c != ',') {
				bytes.write(c);
			} else {
				stringArray[j++] = bytes.toString();
				bytes.reset();
			}
		}
		stringArray[j++] = bytes.toString();
		bytes.reset();
		return stringArray;
	}

	public static int[][] Fonction(int[][] ranges, int num, int rowCount,
			int x, int y, int width, int height, int offsetX, int offsetY) {
		if (ranges != null) {
			return ranges;
		}
		ranges = new int[num][rowCount];
		int lastX = x;
		int lastY = y;

		int OneNum = num - (num % rowCount);
		int count = 0;
		for (int i = 0; i < OneNum; i++) {
			ranges[i][0] = x; // X
			ranges[i][1] = y; // Y
			ranges[i][2] = width; // width
			ranges[i][3] = height; // height

			x += offsetX;
			y += offsetY;
			count++;
			if ((i != 0) && (count % rowCount == 0)) {

				x = lastX;
				y = lastY + height * 2;
				lastY = y;
			}
		}

		for (int i = OneNum; i < num; i++) {
			ranges[i][0] = x; // X
			ranges[i][1] = y; // Y
			ranges[i][2] = width; // width
			ranges[i][3] = height; // height
			x += offsetX;
			y += offsetY;
		}
		return ranges;
	}

	/**
	 * 写英文
	 * 
	 * @param g
	 *            画笔
	 * @param str
	 *            字符串
	 * @param x
	 *            横坐标
	 * @param y
	 *            纵坐标
	 */
	public static void drawEnglish(Bitmap img, Canvas g, String str, int x,
			int y) {
		for (int i = 0, n; i < str.length(); i++) {
			char c = str.charAt(i);

			int sy = 0, sh = 16;// 字符串的的从哪里截和高
			if (c < 97 && c >= 65) { // 大写英文
				n = c - 65;
			} else if (c < 65 && c > 58) {
				n = c - 38;
			} else if (c <= 57 && c >= 48) {// 阿拉伯数字
				n = c - 48;
			}

			else {
				n = 36;

			}
			drawRegion(g, img, n * 16, sy, 16, sh, 0, x, y, 0);
			x += 8;
		}
	}

	/**
	 * 写数字 char 0 =num 48 char 9= num 57
	 * 
	 * @param g
	 *            画笔
	 * @param str
	 *            字符串
	 * @param x
	 *            横坐标
	 * @param y
	 *            纵坐标
	 */

	// 画数字
	public static void drawNum(Bitmap numImage, Canvas g, String str, int x,
			int y) {
		for (int i = 0, n; i < str.length(); i++) {
			char c = str.charAt(i);
			int sy = 0, sh = numImage.getHeight();// 字符串的的从哪里截和高
			int sx = numImage.getWidth() / 11;
			if (c < 58) {
				n = c - 48;

			} else {
				n = c - 97;
			}
			if (c == ':') {
				n = 10;
			}
			drawRegion(g, numImage, n * sx, 0, sx, sh, 0, x, y, 0);
			x += sx;
		}
	}

	// 画黄色数字
	public static void drawYellowNum(Bitmap numImage, Canvas g, String str,
			int x, int y) {
		for (int i = 0, n; i < str.length(); i++) {
			char c = str.charAt(i);
			int sy = 0, sh = numImage.getHeight();// 字符串的的从哪里截和高
			int sx = numImage.getWidth() / 10;
			if (c < 58 && c > 48) {
				n = c - 48 - 1; // 1在第一位
			} else if (c <= 48) {
				n = c - 48 + 9;
			} else {
				n = 10;
			}
			drawRegion(g, numImage, n * sx, 0, sx, sh, 0, x, y, 0);
			x += sx;
		}
	}

	/**
	 * 写英文
	 * 
	 * @param g
	 *            画笔
	 * @param str
	 *            字符串
	 * @param x
	 *            横坐标
	 * @param y
	 *            纵坐标
	 */
	public static void drawSmallEnglish(Bitmap img, Canvas g, String str,
			int x, int y) {
		for (int i = 0, n; i < str.length(); i++) {
			char c = str.charAt(i);
			int sy = 0, sh = 12;// 字符串的的从哪里截和高
			if (c - 97 <= 26 && c - 97 >= 0) {
				n = c - 97;
			} else {
				n = 36;
			}
			drawRegion(g, img, n * 12, sy, 12, sh, 0, x, y, 0);
			x += 7;
		}
	}

	public static void drawRegion(Canvas canvas, Bitmap img, int x_src,
			int y_src, int width, int height, int transform, int x_dst,
			int y_dst, int anchor) {
		// if (x_src + width > img.getWidth() || y_src + height >
		// img.getHeight()
		// || width < 0 || height < 0 || x_src < 0 || y_src < 0) {
		// throw new IllegalArgumentException("Area out of Image");
		// }

		tmpMatrix.reset();
		int dW = width, dH = height;
		switch (transform) {
		case 0: {
			tmpMatrix.setTranslate(x_dst - x_src, y_dst - y_src);
			break;
		}
		case 2: {
			tmpMatrix.setScale(-1, 1);
			tmpMatrix.postTranslate(x_dst + x_src + width, y_dst - y_src);
			break;
		}
		case 5: {
			tmpMatrix.setRotate(90);
			tmpMatrix.postTranslate(y_src + x_dst + height, y_dst - x_src);
			dW = height;
			dH = width;
			break;
		}
		case 7: {
			tmpMatrix.setScale(-1, 1);
			tmpMatrix.postRotate(90);
			tmpMatrix.postTranslate(x_dst + y_src + height, y_dst + x_src
					+ width);
			dW = height;
			dH = width;
			break;
		}
		case 3: {
			tmpMatrix.setRotate(180);
			tmpMatrix.postTranslate(x_dst + width + x_src, y_dst + height
					+ y_src);
			break;
		}
		case 6: {
			tmpMatrix.setRotate(270);
			tmpMatrix.postTranslate(x_dst - y_src, y_dst + width + x_src);
			dW = height;
			dH = width;
			break;
		}
		case 1: {
			tmpMatrix.setScale(-1, 1);
			tmpMatrix.postRotate(180);
			tmpMatrix.postTranslate(x_dst - x_src, y_dst + height + y_src);
			break;
		}
		case 4: {
			tmpMatrix.setScale(-1, 1);
			tmpMatrix.postRotate(270);
			tmpMatrix.postTranslate(x_dst - y_src, y_dst - x_src);
			dW = height;
			dH = width;
			break;
		}
		default:
			throw new IllegalArgumentException("Bad transform");
		}

		if ((anchor & 32) != 0) {
			tmpMatrix.postTranslate(0, -dH);
			y_dst -= dH;
		} else if ((anchor & 2) != 0) {
			tmpMatrix.postTranslate(0, -(dH >> 1));
			y_dst -= dH >> 1;
		}

		if ((anchor & 8) != 0) {
			tmpMatrix.postTranslate(-dW, 0);
			x_dst -= dW;
		} else if ((anchor & 1) != 0) {
			tmpMatrix.postTranslate(-(dW >> 1), 0);
			y_dst -= dW >> 1;
		}

		canvas.save();
		canvas.clipRect(x_dst, y_dst, x_dst + dW, y_dst + dH);
		canvas.drawBitmap(img, tmpMatrix, null);
		canvas.restore();
	}

	public static byte getRangesIndex(int[][] ranges, float x, float y) {
		if (ranges == null) {
			return -1;
		}
		if (x > 0 && y > 0) {// 判断所选择的位置
			for (byte i = 0; i < ranges.length; i++) {
				if (x > ranges[i][0] && y > ranges[i][1]
						&& x < ranges[i][0] + ranges[i][2]
						&& y < ranges[i][1] + ranges[i][3]) {
					return i;
				}
			}
		}
		return -1;
	}

	public static void drawRange(Canvas g, Bitmap img, int x, int y, int srcx,
			int srcy, int srcw, int srch) {
		g.getClipBounds(tempRec);
		g.clipRect(x, y, x + srcw, y + srch, Op.INTERSECT);
		g.drawBitmap(img, x - srcx, y - srcy, null);
		g.clipRect(tempRec, Op.REPLACE);
	}

	/**
	 * 创建功能块
	 * 
	 * @param array
	 *            数组
	 * @param num
	 *            个数
	 * @param x
	 *            X点
	 * @param y
	 *            Y点
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @param offsetX
	 *            X坐标偏移
	 * @param offsetY
	 *            Y坐标偏移
	 */
	public static int[][] creatFunction(int[][] array, int num, int x, int y,
			int width, int height, int offsetX, int offsetY) {
		if (array != null) {
			return array;
		}
		array = new int[num][4];
		for (int i = 0; i < array.length; i++) {
			array[i][0] = x; // x
			array[i][1] = y; // y
			array[i][2] = width; // width
			array[i][3] = height; // height
			x += offsetX;
			y += offsetY;
		}
		return array;
	}

	public static byte checkRanges(int[][] ranges, int x, int y) {
		for (byte i = 0; i < ranges.length; i++) {
			if (x > ranges[i][0] && y > ranges[i][1]
					&& x < ranges[i][0] + ranges[i][2]
					&& y < ranges[i][1] + ranges[i][3]) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 处理阴影
	 * 
	 * @param g
	 * @param text
	 * @param textColor
	 * @param shadowColor
	 * @param x
	 * @param y
	 * @param paint
	 */
	public static void drawShadow(Canvas g, String text, int textColor,
			int shadowColor, int x, int y, Paint paint) {
		paint.setColor(shadowColor);
		g.drawText(text, x + 1, y, paint);
		g.drawText(text, x - 1, y, paint);
		g.drawText(text, x, y + 1, paint);
		g.drawText(text, x, y - 1, paint);
		paint.setColor(textColor);
		g.drawText(text, x, y, paint);
	}
}
